//
//  PageViewController.m
//  UIScrollView&PageControl开机引导
//
//  Created by LZXuan on 15-5-14.
//  Copyright (c) 2015年 轩哥. All rights reserved.
//

#import "PageViewController.h"
#import "RootViewController.h"
#import "AppDelegate.h"
@interface PageViewController () <UIScrollViewDelegate>
{
    UIScrollView *_scrollView;
}
@end

@implementation PageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatScrollView];
}
- (void)creatScrollView {
    _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    //粘贴内容图片 并排粘贴到scrollView上
    for (NSInteger i = 0; i < 4; i++) {
        NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"34_%d",i+1] ofType:@".png"];
        //加载图片
        UIImage *image = [UIImage imageWithContentsOfFile:path];
        //图片的大小 设置成滚动视图的大小
        UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(i*_scrollView.bounds.size.width, 0, _scrollView.bounds.size.width, _scrollView.bounds.size.height)];
        imageview.image = image;
        //最后一张有一个开始体验按钮
        if (i == 3) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            button.bounds = CGRectMake(0, 0, 400, 50);
            button.center = self.view.center;
          //  button.backgroundColor = [UIColor grayColor];
            [button setTitle:@"周游世界" forState:UIControlStateNormal];
            
        
            button.titleLabel.font=[UIFont systemFontOfSize:60];
            [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            
            [imageview addSubview:button];
            //打开用户交互
            imageview.userInteractionEnabled = YES;
        }
        [_scrollView addSubview:imageview];
      
    }
    //要想滚动还需要设置滚动范围
    //15张图片的宽
    _scrollView.contentSize = CGSizeMake(4*_scrollView.bounds.size.width, _scrollView.bounds.size.height);
    _scrollView.bounces = NO;
    
    //设置按页滚动(按照滚动视图的宽)
    _scrollView.pagingEnabled = YES;
    
    _scrollView.delegate = self;
    
    [self.view addSubview:_scrollView];
    
    //页码指示器
    UIPageControl *pageControl  = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height-30, self.view.bounds.size.width, 30)];
    //pageControl.backgroundColor = [UIColor blackColor];
    //设置小白球的个数
    pageControl.numberOfPages = 4;
    //设置小白球的颜色和当前小白球的颜色
    //pageControl.pageIndicatorTintColor = [UIColor blueColor];
    //pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    pageControl.tag = 301;
    [pageControl addTarget:self action:@selector(pageClick:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:pageControl];
  
}

#pragma mark - 滚动协议
//减速停止的时候 调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    //减速停止 算出 当前的页码
    UIPageControl *page = (UIPageControl *)[self.view viewWithTag:301];
    //内容偏移量/宽---》当前页码
    //修改当前页码
    page.currentPage = scrollView.contentOffset.x/scrollView.bounds.size.width;
}

//页码指示器的 页码 改变的时候调用
- (void)pageClick:(UIPageControl *)page {
    //修改滚动视图的内容偏移量 (内容就会滚动)
    //滚动 偏移 一页
    //_scrollView.contentOffset = CGPointMake(page.currentPage*_scrollView.bounds.size.width, 0);
    //内容偏移带滚动动画
    //[_scrollView setContentOffset:CGPointMake(page.currentPage*_scrollView.bounds.size.width, 0) animated:YES];
    //或者使内容滚动到可见位置
    [_scrollView scrollRectToVisible:CGRectMake(page.currentPage*_scrollView.bounds.size.width, 0, _scrollView.bounds.size.width, _scrollView.bounds.size.height) animated:YES];
}

- (void)btnClick:(UIButton *)button {
    //跳转到真正的app 界面
    RootViewController *rvc = [[RootViewController alloc] init];
    
    
    //模态跳转
    //[self presentViewController:rvc animated:YES completion:nil];
    UINavigationController *vc=[[UINavigationController alloc] initWithRootViewController:rvc];
  // [self.navigationController pushViewController:rvc animated:YES];
    AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
    UIWindow *window = appDelegate.window;
    window.rootViewController = vc;
    NSLog(@"点击button");
   
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
